'use strict';

angular.module('oaApp')
  .controller('AdminUsersCtrl', function ($scope, $http, Auth, User) {

    // Use the User $resource to fetch all users
    $scope.users = User.query();

    $scope.delete = function(user) {
      if(confirm('该操作无法撤销，请确认你是否真的想删除该员工？')){
        User.remove({ id: user._id });
        angular.forEach($scope.users, function(u, i) {
          if (u === user) {
            $scope.users.splice(i, 1);
          }
        });
      }
    };
  })
  .controller('AdminDepartmentsCtrl', function ($scope, $http) {

    $scope.departments = [];
    $scope.department = {};
    $scope.formShow = false;

    $scope.create = function () {
      $http.post('/api/departments', $scope.department)
        .success(function (department) {
          $scope.departments.push(department);
        });
      $scope.formShow = false;
    };

    $scope.delete = function (id, idx) {
      if(confirm('该操作无法撤销，你确定要删除该部门吗？')){
        $http.delete('/api/departments/'+id)
          .success(function () {
            $scope.departments.splice(idx, 1);
          })
          .error(function (err) {
            console.log(err);
          });
      }
    };

    $http.get('/api/departments')
      .success(function (res) {
        $scope.departments = res;
      })
      .error(function (err) {
        console.log(err);
      });
  })
  .controller('AdminUsersAddCtrl', function ($scope, $http, Auth, $location) {
    $scope.user = {};
    $scope.errors = {};
    $scope.departments = [];

    $http.get('/api/departments').success(
      function(departments) {
        $scope.departments = departments;
      }
    );

    $scope.register = function(form) {
      $scope.submitted = true;

      if(form.$valid) {
        Auth.createUser({
          name: $scope.user.name,
          email: $scope.user.email,
          phone: $scope.user.phone,
          department: $scope.user.department,
          role: $scope.user.role,
          registed_at: $scope.user.registed_at,
          password: $scope.user.password
        })
          .then( function() {
            // Account created, redirect to home
            $location.path('/');
          })
          .catch( function(err) {
            err = err.data;
            $scope.errors = {};

            // Update validity of form fields that match the mongoose errors
            angular.forEach(err.errors, function(error, field) {
              form[field].$setValidity('mongoose', false);
              $scope.errors[field] = error.message;
            });
          });
      }
    };
  })

  .controller('AdminUsersEditCtrl', function ($scope, $http, $stateParams, $state) {
    $scope.user = {};
    $scope.user.department = {};
    $scope.errors = {};
    $scope.departments = [];
    $scope.originalDepartment = null;


    $http.get('/api/departments').success(
      function (departments) {
        $scope.departments = departments;
      }
    );

    $http.get('/api/users/'+$stateParams.userId)
      .success(function (res) {
        $scope.user = res;
      });

    $scope.update = function (form) {
      $scope.submitted = true;

      if(form.$valid) {
        var user = angular.copy($scope.user);
        console.log(user);
        $http.put('/api/users/'+$stateParams.userId, user)
          .success(function (res) {
            console.log(res);
            $state.go('app.admin-users');
          })
      }
    };

    //$scope.test1 = { "_id": "54ac952b39cdd7a405e68332", "name": "综合部", "__v": 0, "updated_at": "2015-01-07T02:08:43.088Z", "created_at": "2015-01-07T02:08:43.088Z" };

  });
